package com.huluobo.hyj.hyjmodel.widet

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.huluobo.hyj.hyjmodel.R
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.hyphenate.util.DateUtils
import kotlinx.android.synthetic.main.view_receive_message_item.view.*
import java.util.*

/**
 *@author : USER
 *@date : 2020/9/18 14:08
 *@params :
 *@description:
 */
class ReceiveMessageitemView(context: Context, attrs: AttributeSet? = null):
    RelativeLayout(context,attrs)  {
    fun bindView(emMessage: EMMessage, showTimesTamp: Boolean) {
        updateMessage(emMessage)
        updateTimestamp(emMessage, showTimesTamp)
    }

    private fun updateTimestamp(emMessage: EMMessage, showTimestamp: Boolean) {
        if (showTimestamp) {
            timestamp.visibility = View.VISIBLE
            timestamp.text = DateUtils.getTimestampString(Date(emMessage.msgTime))
        } else timestamp.visibility = View.GONE
    }

    private fun updateMessage(emMessage: EMMessage) {
        if (emMessage.type == EMMessage.Type.TXT) {
            receiveMessage.text = (emMessage.body as EMTextMessageBody).message
        } else {
            //其他消息类型均不显示
            receiveMessage.text = context.getString(R.string.no_text_message)
        }
    }

    init {
        View.inflate(context, R.layout.view_receive_message_item,this)
    }
}